package com.ymm.handler.exception;

import com.ymm.domain.vo.ResponseResult;
import com.ymm.enums.AppHttpCodeEnum;
import com.ymm.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.impl.SizeException;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.ConnectException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SystemException.class)
    public ResponseResult systemExceptionHandler(SystemException e) {
        //封装错误信息返回
        return ResponseResult.errorResult(e.getCode(), e.getMsg());
    }

    @ExceptionHandler({ConnectException.class, RedisConnectionFailureException.class})
    public ResponseResult connectExceptionHandler(Exception e) {
        log.error("出现了异常:{}", e);
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_SERVER_CONNECT_ERROR);
    }

    /**
     * 文件大小异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(SizeException.class)
    public ResponseResult exceptionHandler(Exception e) {
        return ResponseResult.errorResult(AppHttpCodeEnum.FILE_SIZE_ERROR);
    }

}
